package com.yenlien.traceyun.usercenter.dao;


import com.yenlien.traceyun.usercenter.bean.MenuTree;
import com.yenlien.traceyun.usercenter.entity.Resource;
import com.yenlien.traceyun.webase.dao.BaseJpaDao;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

public interface ResourceDao extends BaseJpaDao<Resource> {

    @Query("select r from Resource r where   r.appId=?1 order by r.sortNum asc")
    public List<Resource> findAllMenus(String appId);

    public List<Resource> findByParentId(String parentId);

    @Query("select r from Resource r where  r.appId=?1 and (r.parentId is null or r.parentId ='')  order by r.sortNum asc")
    public List<Resource> findAllRootMenus(String appId);


    @Query("select r from Resource r where r.id in (select ur.resourceId from  UserResource ur where ur.userId = ?1) and r.appId=?2 order by r.sortNum asc")
    public List<Resource> findByUserId(String userId, String appId);

    @Query("select r from Resource r where r.id in (select rc.resourceId from ResourceCustomAppVersion rc where rc.customAppVersionId in(select cc.customAppVersionId from  CompanyCustomAppVersion cc where cc.companyId=?1 )) order by r.sortNum asc ")
    public List<Resource> findByCompanyId(String companyId);

    @Query("select new com.yenlien.traceyun.usercenter.bean.MenuTree(r,a.name,a.id,a.used,a.url,a.code) from Resource r,App a where r.id in (select rc.resourceId from ResourceCustomAppVersion rc where rc.customAppVersionId in(select cc.customAppVersionId from  CompanyCustomAppVersion cc where cc.companyId=?1 )) and r.appId=a.id order by r.sortNum asc ")
    public List<MenuTree> findTreeByCompanyId(String companyId);

    @Query("select new com.yenlien.traceyun.usercenter.bean.MenuTree(r,a.name,a.id,a.used,a.url,a.code) from Resource r,App a where r.id in (select ur.resourceId from  UserResource ur where ur.userId = ?1) and r.appId=a.id order by r.sortNum asc")
    public List<MenuTree> findByUserId(String userId);

    @Query("select new com.yenlien.traceyun.usercenter.bean.MenuTree(r,a.name,a.id,a.used,a.url,a.code) from Resource r,App a where  r.appId=a.id order by r.sortNum asc")
    public List<MenuTree> findAllMenuTree();

    @Query("select r from Resource r where  r.appId=?1 order by r.sortNum ASC")
    List<Resource> findByAppId(String appId);

    @Query("select new com.yenlien.traceyun.usercenter.bean.MenuTree(r,a.name,a.id,a.used,a.url,a.code) from Resource r,App a where r.id in (select dr.resourceId from  DepartmentResource dr where dr.departmentId = ?1) and r.appId=a.id order by r.sortNum asc")
    public List<MenuTree> findByDepartmentId(String departmentId);

    @Query("select r from Resource r where r.id in (select dr.resourceId from  ResourceCustomAppVersion dr where dr.customAppVersionId = ?1) order by r.sortNum asc")
    public List<Resource> findByCustomAppVersionId(String customAppVersionId);


}
